package mqigdcom.mqigd.ui;

import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import mqigdcom.mqigd.CustomApp;
import mqigdcom.mqigd.R;
import mqigdcom.mqigd.utils.Utils;

public abstract class BaseActivity extends AppCompatActivity {

    // ************************ 控件声明 ************************ //
    private View mStatusBarView;// 状态栏的控件
    private View mLoadErrorLayout;// 页面加载出错的布局
    private View mLoadingLayout;// 正在加载中的布局
    private AnimationDrawable mLoadingAnim;// 加载中的动画
    private TextView mTvLoadingTips;// 加载提示语的显示框

    // ************************ 变量声明 ************************ //
    protected static final String TAG = BaseActivity.class.getSimpleName();
    protected Toast mToast;// 消息提示Toast
    protected int mStatusBarHeight;// 状态栏的高度
    protected WebKeyboardListener mWebKeyboardListener;// WebView的软键盘监听器

    @Override
    public void setContentView(View view) {
    }

    @Override
    public void setContentView(int layoutResID) {
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CustomApp.self.addActivity(this);
        super.setContentView(R.layout.activity_base);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }

        LinearLayout rootLayout =(LinearLayout) this.findViewById(R.id.base_root_lly);

        if (Build.VERSION.SDK_INT >= 19) {// 定义一个状态栏，默认隐藏
            mStatusBarView = new View(this);
            mStatusBarView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            mStatusBarHeight = Utils.getStatusBarHeight(this);
            rootLayout.addView(mStatusBarView, 0, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, mStatusBarHeight));
        }

        FrameLayout contentLayout = (FrameLayout)this.findViewById(R.id.base_content_fly);
        contentLayout.addView(createContentView(), 0, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

        if (this instanceof MainWebActivity) {
            mWebKeyboardListener = new WebKeyboardListener(rootLayout, mStatusBarHeight);// 主界面全屏时，WebView软键盘遮挡问题
            mWebKeyboardListener.init();
        }

        mLoadErrorLayout = this.findViewById(R.id.load_error_root_fly);
        mLoadErrorLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleClickedErrorView();
            }
        });
        mLoadingLayout = this.findViewById(R.id.loading_root_lly);
        mLoadingLayout.setOnClickListener(null);
        ImageView ivLoading = (ImageView)mLoadingLayout.findViewById(R.id.loading_anim_iv);
        mLoadingAnim = (AnimationDrawable) ivLoading.getBackground();
        mTvLoadingTips = (TextView) mLoadingLayout.findViewById(R.id.loading_tips_tv);
    }

    /**
     * 创建内容布局
     *
     * @return View控件
     */
    protected abstract View createContentView();

    /**
     * 处理点击了加载失败的控件
     */
    protected abstract void handleClickedErrorView();

    /**
     * 设置自定义的状态栏是否显示
     *
     * @param visible 是否显示
     */
    protected void setCustomStatusBarVisible(boolean visible) {
        if (mStatusBarView != null) {
            mStatusBarView.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 设置加载中的布局是否显示
     *
     * @param visible 是否显示
     */
    protected void setLoadingVisible(boolean visible) {
        setLoadingVisible(visible, null);
    }

    /**
     * 设置加载中的布局是否显示
     *
     * @param visible 是否显示
     * @param tips    提示语
     */
    protected void setLoadingVisible(boolean visible, String tips) {
        if (visible) {
            mLoadingLayout.setVisibility(View.VISIBLE);
            mLoadingAnim.start();
            mTvLoadingTips.setText(tips);
            mTvLoadingTips.setVisibility(!TextUtils.isEmpty(tips) && tips.trim().length() > 0 ? View.VISIBLE : View.GONE);
        } else {
            mLoadingLayout.setVisibility(View.GONE);
            mLoadingAnim.stop();
            mTvLoadingTips.setText(null);
            mTvLoadingTips.setVisibility(View.GONE);
        }
    }

    /**
     * 判断加载中的布局是否显示
     *
     * @return true 显示，false 不可见
     */
    protected boolean isLoadingVisible() {
        return mLoadingLayout.getVisibility() == View.VISIBLE;
    }

    /**
     * 设置加载失败的布局是否显示
     *
     * @param visible 是否显示
     */
    protected void setLoadErrorVisible(boolean visible) {
        mLoadErrorLayout.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 弹出消息提示
     *
     * @param msg      消息文本
     * @param duration 显示时长，长或短
     * @param gravity  弹出位置，上、中、下
     */
    protected void showToast(String msg, int duration, int gravity) {
        cancelToast();
        if (this.isFinishing() || TextUtils.isEmpty(msg)) {
            return;
        }
        mToast = Toast.makeText(this, msg, duration);
        mToast.setGravity(gravity, 0, gravity == Gravity.BOTTOM ? 50 : 0);
        mToast.show();
    }

    /**
     * 取消提示
     */
    protected void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
    }

    @Override
    protected void onDestroy() {
        CustomApp.self.removeActivity(this);
        cancelToast();
        super.onDestroy();
    }
}
